import { Component, OnInit } from '@angular/core';
import { Book } from '../book';
import { BOOKLIST } from '../mock-books';
import { BookService } from '../book.service';
@Component({
  selector: 'app-books',
  templateUrl: './books.component.html',
  styleUrls: ['./books.component.css']
})
export class BooksComponent implements OnInit {

  books:Book[];

  selectedBook:Book;

  constructor(private bookService: BookService) { }

  ngOnInit() {
    this.getBookList();
  }

  getBookList():void {
    this.bookService.getBook().subscribe((data)=>{
      this.books = data;
    });
  }

  add(name: string): void{
    name = name.trim();
    if(!name){return;}
    this.bookService.addBook({name} as Book).subscribe(book=>{
      this.books.push(book);
    });
  }

  delete(book:Book): void{
    this.bookService.deleteBook(book).subscribe(()=>{
      this.books = this.books.filter(b => b !== book);
    });
  }

}
